 <?php
use Lib\PasswordHash;
use Think\Cache;
	 /*
	 *后台数据查询一条数据
	 *
	 */
	function _getOne($table,$data,$arr='',$type=0,$cache=''){
		$where = array();
		if (!session('admin_id') && !$data) {
			die('error');
		} else {
			
			if ($type==1) {	
				//后台登录
				$where['name'] = $data['username'];
				$Userinfo = M($table)->where($where)->find();
				$user_pwd = $Userinfo["passwd"];
				$psw = MD5(MD5($data['password'])) ;				
				$ph = new PasswordHash();				
				if (!$ph->CheckPassword($psw,$user_pwd)) {
					return null;
				}
			} elseif ($type==2) {
				$where['id'] = $data;				
			} else {
				$where = $data;
			}
			//S($cache,null);

			$cache && $list = S($cache);// 获取缓存数据
			if (!$list) {
				if (is_array($where)) {
					$list = M($table)->where($where)->find();
				} else {
					$list = M($table)->where($where,$arr)->find();
				}					
				$cache && S($cache,$list);
			}
			
			if ($type==1) {				
				$data_a['current_ip'] = get_client_ip() ;
				$data_a['last_ip'] = $list['current_ip'] ;
				$data_a['last_time'] = $list['current_time'] ;
				$data_a['current_time'] = time() ;
				$edit_logim = _getEdit($table,'id=%d',array($list['id']),$data_a) ;	
				session('admin_id',$list['id']);
				session('admin_unme',$list['name']);
				session('gid',$list['gid']);
				$group = M('admingroup')->where('id=%d',array($list['gid']))->find();
				session('gr_status',$group['gr_status']);
				session('timeOver',time());			
			} elseif ($type==2 && !$list) {
				session(null);
			}
			return $list ;			
		}		 
	 }
	 
	/*
	 *分页
	 *%d - double,包含正负号的十进制数（负数、0、正数）
	 *%s - string,字符串
	 *%f - float,浮点数
	 *
	 */
	function Page($table,$where,$data='',$num,$join='',$field="*"){
				
			unset($User) ;
			unset($count) ;
			$User =  M($table) ;
			//dump($User);die;
			if (is_array($where)) {
				$count = $User->join($join)->where($where)->count();// 查询满足要求的总记录数
			} else {
				$count = $User->join($join)->where($where,$data)->count();// 查询满足要求的总记录数
			}
		
		$Page       = new \Think\Page($count,$num);// 实例化分页类 传入总记录数和每页显示的记录数
		$show	= $Page->_show();// 分页显示输出
		
		return $show ;					 
	 }	 

	/*
	 *分页
	 *%d - double,包含正负号的十进制数（负数、0、正数）
	 *%s - string,字符串
	 *%f - float,浮点数
	 *
	 */
	function PageJoin($table,$where,$data='',$num,$join,$field="*"){
				
		$join1 = $join['join1'] ;
		$join['join2'] && $join2 = $join['join2'] ;
		$join['join3'] && $join3 = $join['join3'] ;
		$join['join4'] && $join4 = $join['join4'] ;
		$join['join5'] && $join5 = $join['join5'] ;
		unset($count) ;
		$count = M()->field($field)->table($table)->join($join1)->join($join2)->join($join3)->join($join4)->join($join5)->where($where,$data)->count();
		
		$Page       = new \Think\Page($count,$num);// 实例化分页类 传入总记录数和每页显示的记录数
		$show	= $Page->_show();// 分页显示输出
		
		return $show ;					 
	 }		 
	/*
	 *判断是否登录
	 */
	function error_dump(){
		header("Content-Type: text/html; charset=UTF-8");
		
		if (!session('admin_id')) {
			return 0;
		}		 
	 }	 
	 
	/*
	 *清楚缓存
	 *
	 */
	function clear(){
		header("Content-Type: text/html; charset=UTF-8");
		
		if (!session('admin_id') && !$data) {
			die('error');
		}		 
	 }		 	 
	 
	/*
	*
	*子级菜单导航
	*/
	function menuSon($pid,$table,$cache,$order) {
			//S($cache,null);
			$str = "set_id>%d and set_stata>=%d and set_stata<=%d and set_pid=%d";
			$data = array(0,1,2,$pid);
			$list = _getList($table,$str,$data,'',$order);
			if ($_SESSION['gr_status']!='2') {
				unset($url) ;
				$url = M('admingroup')->where('id=%d',array($_SESSION['gid']))->field('rules')->find();
				
				if ($url) {
					$set_url = unserialize ($url['rules']) ;
					
					foreach ($list as $key=>$v) {
							
						foreach ($set_url as $kk=>$vv) {
							
							if ($vv===$v['set_url']) {
								$set_L[] = $v ;
							}							
						}
						unset($list);
					}
					$list = $set_L ;
					//dump($set_L);die;
				} else {
					$list = null ;
				}				
			}
			//dump($list);die;
			return $list;
	}	 
	 
	/*
	*
	*父级导航
	*/
	function menuParent() {
		   //菜单导航
		   unset($str) ;
		   unset($data) ;
		   $str = "set_stata=%d and set_power>=%d and set_power<=%d and 	set_pid=%d";
		   $data = array(1,1,3,0);
		   $parent = _getList('urlset',$str,$data,'admmenu','set_id asc');
				
			if ($_SESSION['gr_status']!='2') {
				
				//菜单导航
				$str = "set_stata=%d and set_power>=%d and set_power<=%d and set_pid>%d";
				$data = array(1,1,2,0);
				$list = _getList('urlset',$str,$data,'','set_pid asc');
				unset($url) ;
				$url = M('admingroup')->where('id=%d',array($_SESSION['gid']))->field('rules')->find();
				
				if ($url) {
					$set_url = unserialize ($url['rules']) ;
					
					foreach ($list as $key=>$v) {
							
						foreach ($set_url as $kk=>$vv) {
							
							if ($vv===$v['set_url']) {
								$set_L[] = $v ;
							}							
						}
						unset($list);
					}
					$list = $set_L ;
					
				} else {
					$list = null ;
				}
				$pid = 0 ; 
				$i = 0 ;
				foreach	($parent as $key=>$v) {
					foreach ($list as $kk=>$vv) {
						if (!$pid) {
							$pid = $vv['set_pid'] ;
							$i = $i+1;
						}
						if ($pid != $vv['set_pid']) {
							$pid = $vv['set_pid'] ;
							$i = $i+1;
						}
						if ($v['set_id']===$pid && $i>0) {
							$i = 0;
							$mune_L[] = $v ;
						}
					}
				}
				
			} else {
				$mune_L = $parent ;
			}
			//dump($mune_L);die;
			return $mune_L;
	}
	
	/*
	 *强制刷新
	 */
	function _Refresh($get,$cache){
		
		if ($get) {
			S('clear',$get,600);	
		}
		if (S('clear')) {
			S($cache,null);
		}		 
	 }		 
	 
	/*
	 *超时强制退出
	 */
	function timeOver(){
		
		$over =30*60;//30分钟后强制退出
		$time = time();
		
		$getTime = $_SESSION['timeOver'];	
		$nowT = $time - $getTime ;
		
		if ($nowT>$over && $_SESSION['timeOver']) {
			session(null);	
			return 0;
		} elseif($nowT<$over && $_SESSION['timeOver']) {
			session('timeOver',time());
			return 1;
		} else {
			return 0;
		}
	 }	

	

	 
	 